package com.xsm.ppsecret.widgets;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.xsm.ppsecret.R;

/**
 * Author: Xiasem
 * Email: xiasem@163.com
 * Date: 17-4-7.
 */

public class FindUserDialog extends Dialog implements View.OnClickListener {
    private static int defaultHeight = 285;
    private static int defaultWidth = 285;
    private Context mContext;
    private FindUserListener mFindUserListener;
    private EditText mEditUsername;
    private EditText mEditPassword;

    public FindUserDialog(Context context, int style, FindUserListener findUserListener) {
        this(context, defaultWidth, defaultHeight, style);
        mContext = context;
        mFindUserListener = findUserListener;
    }

    public FindUserDialog(Context context, int width, int height, int style) {
        super(context, style);
        //加载布局
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        if (height != 0) {
            params.height = height;
        } else {
            params.height = defaultHeight;
        }
        if (width != 0) {
            params.width = width;
        } else {
            params.width = defaultWidth;
        }
        window.setAttributes(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.dialog_ease_login, null);
        this.setContentView(view);
        TextView cancel = (TextView) view.findViewById(R.id.dialog_cancel);
        TextView confirm = (TextView) view.findViewById(R.id.dialog_confirm);
        mEditUsername = (EditText) view.findViewById(R.id.username_edit);
        cancel.setOnClickListener(this);
        confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_cancel:
                this.dismiss();
                break;
            case R.id.dialog_confirm:
                String username = mEditUsername.getText().toString();
                mFindUserListener.find(username);
                this.dismiss();
                break;
        }
    }

    public interface FindUserListener {
        void find(String username);
    }
}
